package it.luigiarpino.sudoku;

import java.lang.Integer;

import javax.swing.JFrame;



public class GuiSchema extends javax.swing.JDialog {
    

	private static final long serialVersionUID = 1L;
	private Sudoku s;
    private int[][] numeri=new int[9][9];
    private javax.swing.JTable jTable1;
    private javax.swing.JPanel Pannello;
    private javax.swing.JPanel pannelloNorth;
    private javax.swing.JButton bottoneAiuto;
    private javax.swing.JButton bottoneSoluzione;
    public boolean vittoria=false;
    
    

    public GuiSchema(java.awt.Frame parent, boolean modal, int difficolta) {
        super(parent, modal);
        // imposta il Look&Feel di sistema 
        try 
        { 
            javax.swing.UIManager.setLookAndFeel(javax.swing.UIManager.getSystemLookAndFeelClassName()); 
        } 
        catch(Exception e) {}
        Components();
        setSize(450,490);
        this.s=new Sudoku(difficolta, false);
        MyTableModelSudoku tSudoku=new MyTableModelSudoku(s);
        jTable1.setAutoCreateColumnsFromModel(true);
        jTable1.setModel(tSudoku);
        //Debug
        jTable1.setRowHeight(40);
        jTable1.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0)));
        jTable1.setFont(new java.awt.Font("SansSerif", 0, 20));
        jTable1.setDefaultRenderer(String.class,new CellRenderer(s));
        this.jTable1.setDefaultEditor(String.class, new CellEditor());
        this.setResizable(false);
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                getFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });
    }// fine costruttore
        
   
    
      

    private void Components() {
        jTable1 = new javax.swing.JTable();

        getContentPane().setLayout(new java.awt.BorderLayout());

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sudoku game");
        jTable1.setCellSelectionEnabled(true);
        jTable1.setPreferredSize(new java.awt.Dimension(270, 270));
        Pannello=new javax.swing.JPanel();
        Pannello.setLayout(new java.awt.BorderLayout());
        Pannello.setBorder(javax.swing.BorderFactory.createEmptyBorder(30,30,30,30));
        Pannello.add(jTable1,java.awt.BorderLayout.CENTER);
        this.pannelloNorth =new javax.swing.JPanel();
        this.pannelloNorth.setLayout(new java.awt.FlowLayout());
        //Font carattere =new Font("SansSerif",Font.PLAIN,16);
       
        //
        this.bottoneAiuto=new javax.swing.JButton("Hint");
        //this.bottoneAiuto.setFont(carattere);
        this.bottoneAiuto.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BottoneAiutoPremuto(evt);
            }
        });
        // Bottone visualizza soluzione
        this.bottoneSoluzione=new javax.swing.JButton("Show solution");
        //this.bottoneSoluzione.setFont(carattere);
        this.bottoneSoluzione.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BottoneVisualizzaSoluzionePremuto(evt);
            }
        });
        this.pannelloNorth.add(this.bottoneAiuto);
        this.pannelloNorth.add(this.bottoneSoluzione);
        this.getContentPane().add(this.pannelloNorth,java.awt.BorderLayout.NORTH);
        getContentPane().add(Pannello,java.awt.BorderLayout.CENTER);
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        pack();
    }
    
    private void BottoneVisualizzaSoluzionePremuto(java.awt.event.ActionEvent evt) 
    {
    	GuiSoluzione guiSoluzione = new GuiSoluzione(new javax.swing.JFrame(), true ,this.s);
    	guiSoluzione.setLocationRelativeTo(null);
    	guiSoluzione.setVisible(true);
        
    }
    
    private void BottoneAiutoPremuto(java.awt.event.ActionEvent evt) {
        if (this.ceckIsCompleto()){
            this.bottoneAiuto.setVisible(false);
        }
        else{
            this.s.SuggerisciMossa();
        }
        
        this.repaint();
    }// Fine Bottone Aiuto Premuto
    
    private void getFocus(java.awt.event.WindowEvent evt) {
         for (int i=0;i<=8;i++){
            for (int j=0;j<=8;j++){
                Integer numero;
                try{
                    numero=new Integer(jTable1.getValueAt(i,j).toString());
                    numeri[i][j]=numero.intValue();
                }
                catch (java.lang.Exception e){
                    numeri[i][j]=0;
                }
                
            }
        }    
        
        //Debug
         /*
        String stringa=new String();
        for (int i=0;i<=8;i++){
            for (int j=0;j<=8;j++){
                stringa=stringa+" "+String.valueOf(numeri[i][j]);
            }
            stringa=stringa+"\n";
        }
        System.out.println(stringa);
        
        System.out.println("E' completo="+String.valueOf(this.ceckIsCompleto()));
        System.out.println("Colonne="+String.valueOf(this.ceckColonne()));
        System.out.println("Righe="+String.valueOf(this.ceckRighe()));
        System.out.println("Quadrati="+String.valueOf(this.ceckQuadrati()));
        System.out.println("\n\n");
       */
        
        // Controlla se hai vinto
        if (this.ceckIsCompleto()){
            boolean HaiVinto=this.ceckColonne()&&this.ceckRighe()&&this.ceckQuadrati();
            if (HaiVinto){
               this.vittoria=true;
               setVisible(false);
               dispose();
            }
        }
        
    }// Fine getFocus()

    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) 
    {
    	GuiSchema gui= new GuiSchema(new JFrame(), true, 2);
    	gui.setLocationRelativeTo(null);
    	gui.setVisible(true);
    }
    
    private boolean ceckRighe() {
        int somma=0;
        for (int riga=0;riga<=8;riga++){
            for(int j=0;j<=8;j++){
                somma=numeri[riga][j]+somma;
            }
        }
        if (somma==405){
            return true;
        }
        else{
            return false;
        }
    }    
    
    private boolean ceckColonne() {
        //colonna va da zero ad otto
        int somma=0;
        for(int colonna=0;colonna<=8;colonna++){
            for(int i=0;i<=8;i++){
                somma=somma+numeri[i][colonna];
            }
        }
        if (somma==405){
            return true;
        }
        else{
            return false;
        }
    }
    
    private boolean ceckQuadrati() {
        int somma=0;
        for(int min_riga=0;min_riga<=6;min_riga=min_riga+3){
            for(int min_colonna=0;min_colonna<=6;min_colonna=min_colonna+3){
                somma=numeri[min_riga][min_colonna]+numeri[min_riga][min_colonna+1]+numeri[min_riga][min_colonna+2]+somma;
                somma=numeri[min_riga+1][min_colonna]+numeri[min_riga+1][min_colonna+1]+numeri[min_riga+1][min_colonna+2]+somma;
                somma=numeri[min_riga+2][min_colonna]+numeri[min_riga+2][min_colonna+1]+numeri[min_riga+2][min_colonna+2]+somma;
            }
        }
        if (somma==405){
            return true;
        }
        else {
            return false;
        }
    }
    
    private boolean ceckIsCompleto() {
        for (int i=0;i<=8;i++){
            for (int j=0;j<=8;j++){
                if (numeri[i][j]==0){
                    return false;
                }
            }
        }
        return true;
    }
    

    
}
